package com.ky.circle.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.circle.entity.Problem;
import com.ky.circle.mapper.ProblemMapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.ProblemAdd;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author lcr
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/circle/problem")
public class ProblemController {

    @Autowired
    private ProblemMapper problemMapper;



    @RequestMapping("/selectProblemList")
    public R selectProblemList() {
        List<Problem> problems = problemMapper.selectList(null);
        return R.ok(problems);

    }


    @RequestMapping("/selectProblemById/{id}")
    public R selectProblemById(@PathVariable Integer id) {
        QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
        problemQueryWrapper.eq("id", id);
        return R.ok(problemMapper.selectOne(problemQueryWrapper));
    }



    @RequestMapping("/addProblem")
    public R addProblem(HttpServletRequest request, @RequestBody ProblemAdd problemAdd) {
        try {
            // 解析用户ID
            String token = request.getHeader(SystemConfig.HEADER_TOKEN);
            Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();

            // 创建问题对象
            Problem problem = new Problem();
            problem.setTitle(problemAdd.getTitle());
            problem.setMoney(problemAdd.getMoney());
            problem.setContent(problemAdd.getContent());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            problem.setTime(simpleDateFormat.format(new Date()));
            problem.setUserId(id);
            // 插入问题表
            problemMapper.insert(problem);

            System.out.println("problem=====>"+ problem);

            return R.ok(problem);
        } catch (Exception e) {
            return R.fail("失败");
        }
    }



    @RequestMapping("/addFollowers/{id}")
    public R addFollowers(@PathVariable int id){
        QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
        problemQueryWrapper.eq("id", id);
        Problem problem = problemMapper.selectOne(problemQueryWrapper);
        problem.setFollow(problem.getFollow()+1);
        problemMapper.update(problem,problemQueryWrapper);
        return R.ok(problem);
    }



    @RequestMapping("/commentsCount/{id}/commentsCount")
    public R commentCount(@PathVariable int id){
        QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
        problemQueryWrapper.eq("id", id);
        Problem problem = problemMapper.selectOne(problemQueryWrapper);
        problem.setCommentsCount(problem.getCommentsCount()+1);
        problemMapper.update(problem,problemQueryWrapper);

        return R.ok(problem);
    }



    @RequestMapping("/follow/{id}/follow")
    public R selectFollow(@PathVariable int id){
        QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
        problemQueryWrapper.eq("id", id);
        return R.ok(problemMapper.selectOne(problemQueryWrapper));
    }




}

